package com.luhui.codereview.swing

import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.ui.components.JBPanel
import com.intellij.ui.components.JBTabbedPane
import com.luhui.codereview.*
import com.luhui.codereview.actions.CreateUserGroupAction
import com.luhui.codereview.actions.QrCodeAction
import com.luhui.codereview.actions.RefreshAction
import java.awt.*


/**
 * @author luhui
 * @version 1.0
 * @date 2019/10/29 21:39
 */
class CodeReviewPanel(val project: Project) : JBPanel<JBPanel<*>>(){

    val viewTabTable : ViewTabTable.BaseViewTabTable
    val userListTable : UserListTabTable.BaseUserListTable
    val selfUserTab:SelfUserTab
    val createCodeReviewTab:CreateCodeReviewTab


    private var jBTabbedPane:JBTabbedPane? = null
        set(value){
            field = value
            this.viewTabTable?.getApplicationContext()?.jBTabbedPane = value
        }



    companion object {
        fun getInstance(project: Project): CodeReviewPanel {
            return ServiceManager.getService(project, CodeReviewPanel::class.java)
        }
    }

    init {
        this.layout = BorderLayout()

        //审核列表Tab
        val panelCodeReview = JBPanel<JBPanel<*>>()
        panelCodeReview.layout = BorderLayout()
        val viewTabTable = ViewTabTable.buildViewTab(this.project)
        this.viewTabTable = viewTabTable
        panelCodeReview.add(viewTabTable, "First")
        val jTabbedPane = JBTabbedPane()
        jTabbedPane.addTab(getMessage("tab.masterPanelTab.text"), CodeReviewIcon, panelCodeReview)

        //发起审核Tab
        val createCodeReviewTab = CreateCodeReviewTab(this.viewTabTable.getApplicationContext(),this.project)
        this.createCodeReviewTab = createCodeReviewTab
        jTabbedPane.addTab(getMessage("tab.createCodeReviewTab.text"),SaveCodeReviewIcon,createCodeReviewTab)

        //添加当前用户个人中心列表
        val selfUserTab = SelfUserTab()
        this.selfUserTab = selfUserTab
        jTabbedPane.addTab(getMessage("tab.userinfo.text"),UserInfoIcon,selfUserTab)

        //用户列表Tab
        val userListTable = UserListTabTable.buildUserListTab(project)
        this.userListTable = userListTable
        jTabbedPane.addTab(getMessage("tab.tab.title"),null,userListTable)

        this.jBTabbedPane = jTabbedPane
        this.add(jTabbedPane)

        initGroup(viewTabTable)
    }

    private fun initGroup(viewTabTable: ViewTabTable.BaseViewTabTable) {
        val group = DefaultActionGroup("Group", false)
        group.add(RefreshAction(viewTabTable,userListTable))
        group.add(QrCodeAction(project))
        group.add(CreateUserGroupAction(project,jBTabbedPane))

        val actionToolbar = ActionManager.getInstance().createActionToolbar("horizontal", group, true)
        this.add(actionToolbar.component,"First")
    }
}
